﻿//bob.obj
bob.ns.setNs('bob.obj', {

    //v.2
    canExtractInterface: function (objSource, objInterface) {
        if (bob.utils.isUndefinedOrNull(objSource)) {
            throw new Error('Wrong argument: objSource.');
        }
        if (bob.utils.isUndefinedOrNull(objInterface)) {
            throw new Error('Wrong argument: objInterface.');
        }

        return bob.collections.extensions.all.call(objInterface, function (name, member) {
            if (!bob.utils.isFunction(member)) {
                throw new Error('Interface must contain only functions.');
            }

            return bob.utils.isFunction(objSource[name]);
        });
    },

    //v.2
    extractInterface: function (objSource, objInterface, bNoFixedObject) {

        if (bob.utils.isUndefinedOrNull(objSource)) {
            throw new Error('Wrong argument: objSource.');
        }
        if (bob.utils.isUndefinedOrNull(objInterface)) {
            objInterface = bob.collections.extensions.where.call(objSource, function (memberName, member) {
                return bob.utils.isFunction(member);
            });
        }

        var result = bob.collections.extensions.select.call(objInterface, function (fnName, func) {
            if (!bob.utils.isFunction(func)) {
                throw new Error('Interface must contain only functions.');
            }

            if (!bob.utils.isFunction(objSource[fnName])) {
                throw new Error(bob.string.formatString('Cannot extract interface: function "{0}" not found.', fnName));
            }

            if (!bNoFixedObject) {
                return bob.fn.fixedObjectFunction(objSource, fnName);
            }
            else {
                return objSource[fnName];
            }
        });

        return result;
    }

});
